﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using DXApplicationTangche.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;

namespace DXApplicationTangche.UC.订单一览新.辅料.辅料下单
{
    public partial class Form辅料属性 : DevExpress.XtraEditors.XtraForm
    {
        private List<StyleAccessoryDTO> StyleAccessoryDTOs = new List<StyleAccessoryDTO>();
        private DTO辅料一览 dTO辅料一览 = new DTO辅料一览();
        private StyleAccessoryDTO styleAccessoryDTO = new StyleAccessoryDTO();
        private Boolean 修改辅料 = false;
        private String subMaterialPositionId { get; set; }//位置
        private String subMaterialPosition { get; set; }//位置
        private String subMaterialColorId { get; set; }//颜色
        private String subMaterialColor { get; set; }//颜色
        public Form辅料属性(List<StyleAccessoryDTO> dtos,DTO辅料一览 dTO)
        {
            InitializeComponent();
            this.StyleAccessoryDTOs = dtos;
            this.dTO辅料一览 = dTO;
            this.textBox辅料编码.Text = this.dTO辅料一览.subMaterialCode;
            this.textBox辅料名称.Text = this.dTO辅料一览.subMaterialName;
            this.textBox品番.Text = this.dTO辅料一览.subMaterialNumber;
            this.textBox供应商.Text = this.dTO辅料一览.subMaterialSupplier;
            try
            {
                this.pictureEdit图片.Image
                    = FileService.GetImageByUrl(
                        URL.图片url + URL.辅料图片路径 + this.dTO辅料一览.subMaterialPicture
                        );
            }
            catch
            {
                this.pictureEdit图片.Image = FunctionHelper.getSSHIRT();
            }
            this.searchLookUpEdit位置.Properties.DataSource = 辅料Service.get辅料位置(this.dTO辅料一览.subMaterialTypeId);
        }
        public Form辅料属性(StyleAccessoryDTO dto,Boolean 修改辅料)
        {
            InitializeComponent();
            this.修改辅料 = 修改辅料;
            this.styleAccessoryDTO = dto;
            this.textBox辅料编码.Text = this.styleAccessoryDTO.subMaterialCode;
            this.textBox辅料名称.Text = this.styleAccessoryDTO.subMaterialName;
            this.textBox品番.Text = this.styleAccessoryDTO.subMaterialNumber;
            this.textBox供应商.Text = this.styleAccessoryDTO.subMaterialSupplier;
            try
            {
                this.pictureEdit图片.Image
                    = FileService.GetImageByUrl(
                        URL.图片url + URL.辅料图片路径 + this.styleAccessoryDTO.subMaterialPicture
                        );
            }
            catch
            {
                this.pictureEdit图片.Image = FunctionHelper.getSSHIRT();
            }
            this.subMaterialPositionId = this.styleAccessoryDTO.subMaterialPositionId;
            this.colorEdit颜色.Color = ColorTranslator.FromHtml(this.styleAccessoryDTO.subMaterialColor);
            this.subMaterialPositionId = this.styleAccessoryDTO.subMaterialPositionId;
            this.textBox位置.Text = this.styleAccessoryDTO.subMaterialPosition;
            this.textBox单耗.Text = this.styleAccessoryDTO.subMaterialCount.ToString();
            this.richTextBox备注.Text = this.styleAccessoryDTO.subMaterialRemarks;
            this.searchLookUpEdit位置.Properties.DataSource = 辅料Service.get辅料位置(this.styleAccessoryDTO.subMaterialTypeId);
        }
        #region 选择位置
        private void searchLookUpEdit位置_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn位置_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit位置View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit位置.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.subMaterialPositionId = this.searchLookUpEdit位置.Properties.View.GetRowCellValue(rowHandle, "subMaterialPositionId").ToString();
                    this.subMaterialPosition = this.searchLookUpEdit位置.Properties.View.GetRowCellValue(rowHandle, "subMaterialPosition").ToString();
                }
            }
            this.textBox位置.Text = this.subMaterialPosition;
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn位置_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit位置.ToolTip = null;
            searchLookUpEdit位置.EditValue = null;
        }
        private void searchLookUpEdit位置_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.subMaterialPosition;
        }

        #endregion

        private void Form辅料属性_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox单耗;
        }
        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(this.colorEdit颜色.Text))
            {
                Color color = this.colorEdit颜色.Color;
                this.subMaterialColorId = ColorTranslator.ToHtml(color);
                this.subMaterialColor = color.Name;
            }
            if (String.IsNullOrWhiteSpace(this.textBox单耗.Text.Trim()))
            {
                this.ActiveControl = this.textBox单耗;
                MessageBox.Show("请填写单耗（数量）");
                return;
            }
            if (修改辅料)
            {
                this.styleAccessoryDTO.subMaterialPositionId = this.subMaterialPositionId;
                this.styleAccessoryDTO.subMaterialPosition = this.subMaterialPosition;
                this.styleAccessoryDTO.subMaterialColorId = this.subMaterialColorId;
                this.styleAccessoryDTO.subMaterialColor = this.subMaterialColor;
                this.styleAccessoryDTO.subMaterialCount = Convert.ToDouble(this.textBox单耗.Text.Trim());
                this.styleAccessoryDTO.subMaterialRemarks = this.richTextBox备注.Text.Trim();
            }
            else
            {
                this.StyleAccessoryDTOs.Add(
                    new StyleAccessoryDTO(
                        this.dTO辅料一览,
                        this.subMaterialPositionId,
                        this.subMaterialPosition,
                        this.subMaterialColorId,
                        this.subMaterialColor,
                        Convert.ToDouble(this.textBox单耗.Text.Trim()),
                        this.richTextBox备注.Text.Trim()
                    ));
            }
            this.Close();
        }

        private void textBox单耗_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键和负号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
            if (e.KeyChar == (char)('-'))
            {
                if (textBox单耗.Text != "")
                {
                    //MessageBox.Show("请输入正确的数字");
                    this.textBox单耗.Text = "";
                    e.Handled = true;
                }
            }
            //小数点只能输入一次  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
            //第一位不能为小数点  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
            //第一位是0，第二位必须为小数点  
            if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
            //第一位是负号，第二位不能为小数点  
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
        }
    }
}